package dp.state;

import dp.state.interfaces.State;

public class GumballMachine {

	private State soldOutState;
	private State noQuarterState;
	private State hasQuarterState;
	private State soldState;
	private State winnerState;
	
	private State state = soldOutState;
	private int count = 0;
	
	public GumballMachine(int numberGumballs) {
		soldOutState = new SoldOutState(this);
		noQuarterState = new NoQuarterState(this);
		hasQuarterState = new HasQuarterState(this);
		soldState = new SoldState(this);
		winnerState = new WinnerState(this);
		
		if(numberGumballs > 0) {
			state = noQuarterState;
		}
	}
	
	public void insertQuarter() {
		state.insertQuarter();
	}
	
	public void ejectQuarter() {
		state.ejectQuarter();
	}
	
	public void turnCrank() {
		state.turnCrank();
		state.dispense();
	}
	
	void releaseBall() {
		System.out.println("A gumball comes rollin out the slot...");
		if(count!=0)
		{
			count--;
		}
	}

	State getHasQuarterState() {
		return hasQuarterState;
	}

	State getNoQuarterState() {
		return noQuarterState;
	}

	State getSoldOutState() {
		return soldOutState;
	}

	State getWinnerState() {
		return winnerState;
	}

	State getSoldState() {
		return soldState;
	}

	void setState(State state) {
		this.state = state;
	}

	int getCount() {
		return count;
	}
}
